package scoutobahn.xml.automaton;

import org.xml.sax.Attributes;

import scoutobahn.xml.OsmNode;
/**
 * State for an OSM Node.
 * @author mark
 *
 */
public class NodeState extends OsmElementState {
    private long id;
    private double latitude;
    private double longitude;
    /**
     * Constructs a new Node State with the given StateManager
     * and the given OsmStremListener.
     * @param stateManager The StateManager
     * @param listener The OsmItemListener
     */
    public NodeState(final StateManager stateManager,
            final OsmItemStreamManager listener) {
        super(stateManager, "node", listener);
    }

    @Override
    public ParserState startElement(final String nameSpace,
            final String localName, final String qName,
            final Attributes attr) {
        if (localName.equals("node")) {
            this.id = Long.parseLong(attr.getValue("id"));
            this.longitude = Double.parseDouble(attr.getValue("lon"));
            this.latitude = Double.parseDouble(attr.getValue("lat"));
        }
        super.startElement(nameSpace, localName, qName, attr);
        return this;
    }
    @Override
    public ParserState endElement(final String uri,
            final String localName, final String qName) {
        if (localName.equals("node")) {
            OsmNode node = new OsmNode(this.id,
                    super.getTags(), this.longitude, this.latitude);
            super.getItemStreamListener().nodeFound(node);
        }
        return super.endElement(uri, localName, qName);
    }

}
